﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Altbrot.Yawr.Player;
using centrafuse.Plugins;
using Altbrot.Yawr.Entities;

namespace Altbrot.Yawr.Player
{
    public class CFPlayer : IStreamPlayer
    {
        private CFPlugin _pluginBase;
        private int currentChannel;

        public CFPlayer(CFPlugin pluginBase)
        {
            this._pluginBase = pluginBase;
            ((IYawr)pluginBase).SetAudioStreamDataChangeEvent(CF_events_audioStreamDataChange);                        
        }

        #region IStreamPlayer Members

        public event PlayerStatusChangedEventHandler OnStatusTextChanged;
        public event System.Windows.Forms.MethodInvoker OnMediaChanged;

        public StreamPlayerPlayState PlayerState
        {
            get { return StreamPlayerPlayState.wmppsReady; }
        }

        public string MediaName
        {
            get { return string.Empty; }
        }

        public string MediaBitrate
        {
            get { return string.Empty; }
        }

        public void Play(Uri url)
        {            
            this.currentChannel = this._pluginBase.CF_playAudioStream(url.AbsoluteUri, null);
        }

        public void Stop()
        {
            this._pluginBase.CF_controlAudioStream(this.currentChannel, centrafuse.Plugins.CF_AudioAction.Stop);
        }

        public void Pause()
        {
            throw new NotImplementedException();
        }

        #endregion

        private void FireOnStatusTextChanged(string message)
        {
            if (OnStatusTextChanged != null)
                OnStatusTextChanged(message);
        }

        public void CF_events_audioStreamDataChange(object sender, CFPlugin.CFTagInformation e)
        {
            this.FireOnStatusTextChanged(e.Title + " " + e.Track);
        }
    }
}
